package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.ApiTestAssert;
import com.jmb.api.model.entity.ApiTestCase;
import com.jmb.api.service.ApiTestAssertService;
import com.jmb.api.mapper.ApiTestAssertMapper;
import com.jmb.api.service.ApiTestCaseService;
import com.jmb.api.service.ApiTestService;
import com.jmb.api.vo.api.ApiTestAssertVo;
import com.jmb.api.vo.api.ApiTestCaseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Queue;

/**
* @author jinbo
* @description 针对表【api_test_assert】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Slf4j
@Service
public class ApiTestAssertServiceImpl extends ServiceImpl<ApiTestAssertMapper, ApiTestAssert>
    implements ApiTestAssertService{
    @Autowired
    private ApiTestAssertMapper apiTestAssertMapper;
    @Autowired
    private ApiTestService apiTestService;


    @Override
    public void saveAssert(ApiTestAssertVo apiTestAssertVo) {
        LambdaQueryWrapper<ApiTestAssert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestAssert::getAssertName,apiTestAssertVo.getAssertName());
        Long count = apiTestAssertMapper.selectCount(queryWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_ASSERT_NAME);
        }
        // 查询caseId是否存在，如果不存在则提示caseId不存在
        apiTestService.selectByCaseId(apiTestAssertVo.getCaseId());

        apiTestAssertMapper.insert(apiTestAssertVo);
    }

    @Override
    public void editAssert(ApiTestAssertVo apiTestAssertVo) {
        apiTestService.selectByCaseId(apiTestAssertVo.getCaseId());
        LambdaQueryWrapper<ApiTestAssert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ApiTestAssert::getAssertName,apiTestAssertVo.getAssertName())
                .ne(ApiTestAssert::getAssertId,apiTestAssertVo.getAssertId());
        if (apiTestAssertMapper.selectCount(queryWrapper)>0){
            throw new LeaseException(ResultCodeEnum.WEB_API_ASSERT_NAME);
        }
        LambdaUpdateWrapper<ApiTestAssert> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApiTestAssert::getAssertId,apiTestAssertVo.getAssertId());
        apiTestAssertMapper.update(apiTestAssertVo,updateWrapper);

    }

    @Override
    public List<Integer> selectAllAssertId(Integer caseId) {
        LambdaQueryWrapper<ApiTestAssert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestAssert::getCaseId,caseId);
        return apiTestAssertMapper.selectList(queryWrapper).stream().map(ApiTestAssert::getAssertId).toList();
    }

    @Override
    public void removeAssertById(Integer assertId) {
        if (assertId == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        this.removeById(assertId);
    }

    @Override
    public void removeAssertByCaseId(Integer caseId) {
        if (caseId == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<ApiTestAssert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestAssert::getCaseId,caseId);
        this.remove(queryWrapper);
    }
}




